<template>
  <div class="reportFormPage">
    <TopOperation ref="TopOperation" :tabs-list="tabList" :active-val.sync="activeVal" @currentActive="currentActive"
      @reportFormsSubmit="reportFormsSubmit" />
    <div v-show="activeVal === 'lqsdhrdjlb'" v-for="(item, index) in excelTypeMap['lqsdhrdjlb']"
      :key="'lqsdhrdjlb' + index">
      <lqsdhrdjlb ref="lqsdhrdjlb" :excel-id="item"></lqsdhrdjlb>
    </div>
    <div v-show="activeVal === 'lqzrdjlb'" v-for="(item, index) in excelTypeMap['lqzrdjlb']" :key="'lqzrdjlb' + index">
      <lqzrdjlb ref="lqzrdjlb" :excel-id="item"></lqzrdjlb>
    </div>
    <div v-show="activeVal === 'lqycjlnfxjlb'" v-for="(item, index) in excelTypeMap['lqycjlnfxjlb']"
      :key="'lqycjlnfxjlb' + index">
      <lqycjlnfxjlb ref="lqycjlnfxjlb" :excel-id="item"></lqycjlnfxjlb>
    </div>
    <div v-show="activeVal === 'lqbmjrjlb'" v-for="(item, index) in excelTypeMap['lqbmjrjlb']"
      :key="'lqbmjrjlb' + index">
      <lqbmjrjlb ref="lqbmjrjlb" :excel-id="item"></lqbmjrjlb>
    </div>
    <div v-show="activeVal === 'rhlqccwdxjlb'" v-for="(item, index) in excelTypeMap['rhlqccwdxjlb']"
      :key="'rhlqccwdxjlb' + index">
      <rhlqccwdxjlb ref="rhlqccwdxjlb" :excel-id="item"></rhlqccwdxjlb>
    </div>
    <div v-show="activeVal === 'rhlqsssyljlb'" v-for="(item, index) in excelTypeMap['rhlqsssyljlb']"
      :key="'rhlqsssyljlb' + index">
      <rhlqsssyljlb ref="rhlqsssyljlb" :excel-id="item"></rhlqsssyljlb>
    </div>
    <div v-show="activeVal === 'rhlqycjlnfxjlb'" v-for="(item, index) in excelTypeMap['rhlqycjlnfxjlb']"
      :key="'rhlqycjlnfxjlb' + index">
      <rhlqycjlnfxjlb ref="rhlqycjlnfxjlb" :excel-id="item"></rhlqycjlnfxjlb>
    </div>
    <div v-show="activeVal === 'rhlqzfclwhljlb'" v-for="(item, index) in excelTypeMap['rhlqzfclwhljlb']"
      :key="'rhlqzfclwhljlb' + index">
      <rhlqzfclwhljlb ref="rhlqzfclwhljlb" :excel-id="item"></rhlqzfclwhljlb>
    </div>
    <div v-show="activeVal === 'gxlqtxhfjlb'" v-for="(item, index) in excelTypeMap['gxlqtxhfjlb']"
      :key="'gxlqtxhfjlb' + index">
      <gxlqtxhfjlb ref="gxlqtxhfjlb" :excel-id="item"></gxlqtxhfjlb>
    </div>

    <backtop-view :sum="activeVal ? excelTypeMap[activeVal].length : 1" @onModPages="onModPages" />
  </div>
</template>

<script>
import { getBsDataReport, bsDataReportTb, bsDataReportModPages } from "@/api/syrw/bsDataReport";
export default {
  name: "index",
  components: {
    TopOperation: () => import("@/views/reportForms/components/topOperation"), // tab页
    BacktopView: () => import("@/views/reportForms/components/backtopView"), // 回到顶部页
    lqsdhrdjlb: () => import("@/views/reportForms/lq/lqsdhrdjlb"), //  沥青闪点和燃点记录表
    lqzrdjlb: () => import("@/views/reportForms/lq/lqzrdjlb"), // 沥青针入度记录表
    lqycjlnfxjlb: () => import("@/views/reportForms/lq/lqycjlnfxjlb"), // 沥青黏附性记录表
    lqbmjrjlb: () => import("@/views/reportForms/lq/lqbmjrjlb"), // 沥青薄膜加热记录表
    rhlqccwdxjlb: () => import("@/views/reportForms/lq/rhlqccwdxjlb"), // 乳化沥青储存稳定性、破乳速度记录
    rhlqsssyljlb: () => import("@/views/reportForms/lq/rhlqsssyljlb"), // 乳化沥青筛上剩余量、微粒离子电荷记录
    rhlqycjlnfxjlb: () => import("@/views/reportForms/lq/rhlqycjlnfxjlb"), // 乳化沥青与粗集料的黏附性记录
    rhlqzfclwhljlb: () => import("@/views/reportForms/lq/rhlqzfclwhljlb"), // 乳化沥青蒸发残留物含量记录
    gxlqtxhfjlb: () => import("@/views/reportForms/lq/gxlqtxhfjlb"), // 改性沥青弹性恢复、离析试验检测记录表
  },
  data () {
    return {
      reportId: undefined,
      tabList: [],
      form: {
        excelTypeList: {}
      },
      activeVal: "",
      // key为excelTypeId， value为 excelId数组
      excelTypeMap: {}
    };
  },

  created () {
    if (this.$route.query) {
      this.reportId = this.$route.query.reportId;
    }
    this.init();
  },
  methods: {
    // 获取记录表和报告表
    async init () {
      if (!this.reportId) {
        this.$message.error("报告id不能为空");
        return;
      }
      const response = await getBsDataReport(this.reportId);
      this.form = response.data;
      if (!this.form.excelTypeList || this.form.excelTypeList.length == 0) {
        this.tabList = [{
          val: undefined,
          name: undefined
        }];
        return;
      }
      let tabList = [];
      this.excelTypeMap = {};
      this.form.excelTypeList.forEach(item => {
        // 是否包含label
        if (this.excelTypeMap.hasOwnProperty(item.label)) {
          this.excelTypeMap[item.label].push(item.excelId);
        } else {
          this.excelTypeMap[item.label] = [item.excelId];
          tabList.push({
            val: item.label,
            name: item.excelTypeName
          });
        }
      });
      this.tabList = tabList;
      this.$set(this.tabList, tabList);
      if (!this.activeVal) {
        this.activeVal = this.tabList[0].val;
      }
      // this.currentActive(this.activeVal)
    },
    currentActive (val) {
      this.activeVal = val;
    },
    // 批量提交
    async reportFormsSubmit (hasTips = true) {
      let arr = [];
      this.tabList.forEach(item => {
        const refList = this.excelTypeMap[item.val];
        if (!refList || refList.length <= 0) {
          return;
        }
        refList.forEach((element, index) => {
          if (this.$refs[item.val] && this.$refs[item.val][index]) {

            if ('lqzrdjlb' === item.val) {
              // 为针入度list赋值
              this.$refs[item.val][index].form.zrdList = this.$refs[item.val][index].rows;
              // 为延度list赋值
              this.$refs[item.val][index].form.ydList = this.$refs[item.val][index].ydRows;
              // 为软化点list赋值
              this.$refs[item.val][index].form.rhdList = this.$refs[item.val][index].rhdRows;
            }
            if ('lqycjlnfxjlb' === item.val) {
              this.$refs[item.val][index].form.deList = this.$refs[item.val][index].nfxRows;
            }
            if ('lqbmjrjlb' === item.val) {
              this.$refs[item.val][index].form.bm1List = this.$refs[item.val][index].bm1Rows;
              this.$refs[item.val][index].form.bm2List = this.$refs[item.val][index].bm2Rows;
              this.$refs[item.val][index].form.bm3List = this.$refs[item.val][index].bm3Rows;
            }
            if ('rhlqccwdxjlb' === item.val) {
              this.$refs[item.val][index].form.wdxList = this.$refs[item.val][index].ccRows;
            }
            if ('rhlqsssyljlb' === item.val) { // 筛上剩余量
              this.$refs[item.val][index].form.baList = this.$refs[item.val][index].sRows;
            }
            if ('rhlqycjlnfxjlb' === item.val) { // 乳化沥青与粗集料黏附性记录表
              this.$refs[item.val][index].form.nfxList = this.$refs[item.val][index].nfxRows;
            }
            if ('rhlqzfclwhljlb' === item.val) { // 乳化沥青蒸发残留物含量试验检测记录表
              this.$refs[item.val][index].form.baList = this.$refs[item.val][index].clwRows;
            }
            if ('gxlqtxhfjlb' === item.val) { // 乳化沥青蒸发残留物含量试验检测记录表
              this.$refs[item.val][index].form.txhfList = this.$refs[item.val][index].txRows;
              this.$refs[item.val][index].form.rhdList = this.$refs[item.val][index].rhdRows;
            }
            let info = this.$refs[item.val][index].form;
            arr.push({ label: item.val, excelObj: info });
          }
        });
      });
      await bsDataReportTb(arr);
      if (hasTips) {
        this.$modal.msgSuccess("操作成功");
      };
    },
    // 加一页减一页
    async onModPages (modType) {
      // 1先保存
      await this.reportFormsSubmit();
      // 2 加减页
      await this.doModPages(modType);
      // 3 重新加载
      await this.init();
    },
    // 加减页
    async doModPages (modType) {
      const arr = this.excelTypeMap[this.activeVal];
      const data = {
        reportId: this.reportId,
        label: this.activeVal,
        modType: modType,
        excelId: Number(modType) < 0 ? arr[arr.length - 1] : undefined
      };
      await bsDataReportModPages(data);
    }
  },
};
</script>

<style scoped></style>
